<?php
/**
 * Created by PhpStorm.
 * User: G
 * Date: 2021/3/23
 * Time: 下午2:12
 */

namespace app\api\controller;

use app\admin\model\profit\Agentapply;
use app\admin\model\profit\ProfitDetail;
use app\common\controller\Api;
use think\Exception;

class Mem365 extends Api
{
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = [''];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    /**
     * 代理商申请
     * @author G
     * @date 2021-03-23 下午2:19
     */
    public function apply()
    {
        try {
            $p_id = $this->request->post('p_id', 0);
            $p_name = $this->request->post('p_name', '');
            $c_id = $this->request->post('c_id', 0);
            $c_name = $this->request->post('c_name', '');
            $cc_id = $this->request->post('cc_id', 0);
            $cc_name = $this->request->post('cc_name', '');

            $user = $this->auth->getUser();

            $user_id = $user['id'];

            // 验证用户身份
            if ($this->auth->getUser()->group_id != \app\common\model\User::GROUP_NORMAL) {
                throw new Exception('身份信息异常');
            }

            $re_id = 0;  // 邀请人id
            $rere_id = 0;  // 邀请人id
            $p_uid = 0; // 父级id
            $c_uid = 0; // 祖级id

            // 邀请人手机号
            $p_mobile = $this->request->post('p_mobile', '');
            if ($p_mobile) {
                $p_user = \app\admin\model\User::where(['mobile' => $p_mobile])->find();
//                if (!$p_user || $p_user['group_id'] == \app\common\model\User::GROUP_NORMAL) {
//                    throw new Exception('邀请人身份信息异常');
//                }
                // 验证用户身份
                if ($this->auth->getUser()->mobile == $p_mobile) {
                    throw new Exception('邀请人不能是本人手机号');
                }
                // 推荐人的id
                $re_id = $p_user['id'];

                // 推荐人的推荐人
                $rere_user = \app\admin\model\User::where(['id' => $re_id])->find();

                if ($rere_user) {
                    $rere_id = $rere_user['referrer_uid'];
                }
            }

            $parentQuInfo = \app\admin\model\User::where([
                'county_id' => $cc_id,
                'group_id' => 3
            ])->find();
            $parentShiInfo = \app\admin\model\User::where([
                'city_id' => $c_id,
                'group_id' => 4
            ])->find();

            // 层级关系
            $p_uid = $parentQuInfo ? $parentQuInfo['id'] : 0;
            $c_uid = $parentShiInfo ? $parentShiInfo['id'] : 0;

            // 更新个人信息
            \app\admin\model\User::where(['id' => $user_id])->update([
                'group_id' => 2,
                'p_uid' => $p_uid,
                'c_uid' => $c_uid,
                'province_id' => $p_id,
                'province_name' => $p_name,
                'city_id' => $c_id,
                'city_name' => $c_name,
                'county_id' => $cc_id,
                'county_name' => $cc_name,
                'referrer_uid' => $re_id,
                'updatetime' => time(),
            ]);
            $temp_data = [
                'uid' => $user_id,
                'referrer_uid' => $re_id,
                're_referrer_uid' => $rere_id,
                'p_uid' => $p_uid,
                'c_uid' => $c_uid,
                'referrer_price' => $re_id ? 50 : 0,
                're_referrer_price' => $rere_id ? 50 : 0,
                'p_price' => 30,
                'c_price' => 40,
                't_price' => 0,
                'print' => $re_id ? 2 : 1,
                'creat_time' => time(),
                'update_time' => time()
            ];
            $insert_res = ProfitDetail::insert($temp_data);
            if (!$insert_res) {
                throw new Exception('分润失败');
            }
        }catch (Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('购买成功', []);
    }
}